/*
 * TCSS 422 - Spring quarter 2011
 * Team: 	Googlers
 * Members: Deepa Sahni, Krisnil Gounder, Michael Schweiger
 * Date: 	April 7, 2011
 */
package testing;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.LinkedList;
import java.util.List;

import org.junit.Test;

import buffers.Buffer;

/**
 * A JUnit 4 test class for buffers.Buffer.
 * @author Michael Schweiger
 * @version 1.0
 */
public class BufferUnitTest {

	/**
	 * Test creation of default constructor, raw and generic.
	 */
	@Test
	@SuppressWarnings("rawtypes")
	public void testDefaultConstructor() {
		Buffer raw = new Buffer();
		assertNotNull(raw);
		assertEquals(raw.getSize(), 0);
		assertTrue(raw.isEmpty());
		
		Buffer<String> generic = new Buffer<String>();
		assertNotNull(generic);
		assertEquals(generic.getSize(), 0);
		assertTrue(generic.isEmpty());
		
		assertTrue(raw.nullsAllowed());
		assertTrue(generic.nullsAllowed());
	}
	
	/**
	 * Test creation of constructor(boolean).
	 */
	@Test
	public void testBooleanConstructor() {
		Buffer<String> not_allowed = new Buffer<String>(false);
		assertFalse(not_allowed.nullsAllowed());
		
		Buffer<String> allowed = new Buffer<String>(true);
		assertTrue(allowed.nullsAllowed());
		
		assertTrue(not_allowed.isEmpty());
		assertTrue(allowed.isEmpty());
	}
	
	/**
	 * Tests the constructor that takes a list and a boolean with a valid setup.
	 */
	@Test
	public void testListBooleanConstructorValidGeneric() {
		List<String> string_list = new LinkedList<String>();
		Buffer<String> buffer = new Buffer<String>(string_list, false);
		assertTrue(buffer.isEmpty());
		
		//make sure the list added is not the same list that got linked internally
		String test = "test";
		string_list.add(test);
		assertTrue(buffer.isEmpty());
		assertFalse(buffer.nullsAllowed());
	}
	
	/**
	 * Tests the constructor that takes a list and a boolean, giving it a null.
	 */
	@Test
	public void testListBooleanConstructorNull() {
		try {
			new Buffer<String>(null, false);
			fail("Exception wasn't caught!");
		} catch (IllegalArgumentException e) {
		}
	}
	
	/**
	 * Test the constructor that takes a list and a boolean, not using generics.
	 */
	@Test
	@SuppressWarnings("all")
	public void testListBooleanConstructorValidRaw() {
		Buffer buffer = new Buffer(new LinkedList(), false);
		assertNotNull(buffer);
		buffer.add("Hello");
		buffer.add(1);
		assertFalse(buffer.isEmpty());
	}
	
	/**
	 * Tests the add method.
	 */
	@Test
	public void testAdd() {
		Buffer<String> buffer_wNulls = new Buffer<String>(true);
		assertTrue(buffer_wNulls.isEmpty());
		buffer_wNulls.add(null);
		assertFalse(buffer_wNulls.isEmpty());
		buffer_wNulls.add("test");
		assertEquals(buffer_wNulls.getSize(), 2);
		
		Buffer<String> buffer = new Buffer<String>(false);
		assertTrue(buffer.isEmpty());
		buffer.add("Test");
		assertFalse(buffer.isEmpty());
		try {
			buffer.add(null);
			fail("Didn't catch an exception!");
		} catch (IllegalArgumentException e) {
		}
	}
	
	/**
	 * Test the remove method
	 */
	@Test
	public void testRemove() {
		Buffer<String> buffer = new Buffer<String>();
		assertTrue(buffer.isEmpty());
		try {
			buffer.remove();
			fail("Didn't catch an exception!");
		} catch (IllegalStateException e) {
		}
		String test = "Test";
		buffer.add(test);
		assertFalse(buffer.isEmpty());
		assertEquals(test, buffer.remove());
		assertTrue(buffer.isEmpty());
	}
}
